package ru.spbu.math.seabattle.client.jms.gui;

import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import ru.spbu.math.seabattle.client.jms.SeaBattleClient;
import ru.spbu.math.seabattle.entity.GameResult;

public class GameEndedGUI extends JFrame {
	
	private static final long serialVersionUID = 1L;
	
	private static final String AFTER_WINNER_TEXT = " wins!";
	private static final String CONTINUE_BUTTON_TEXT = "Continue";
	private static final String EXIT_BUTTON_TEXT = "Exit";
	
	private static final Rectangle WINDOW_RECTANGLE = new Rectangle(250, 200, 300, 100);
	private static final Rectangle LABEL_RECTANGLE = new Rectangle(10, 10, 280, 20);
	private static final Rectangle LBUTTON_RECTANGLE = new Rectangle(10, 40, 130, 20);
	private static final Rectangle RBUTTON_RECTANGLE = new Rectangle(150, 40, 130, 20);
	
	public GameEndedGUI(final SeaBattleClient client, GameResult result) {
		
		super("Results");
		
		this.getContentPane().setLayout(null);			
		this.setBounds(WINDOW_RECTANGLE);
		this.setVisible(true);
		this.setResizable(false);
		
		JLabel winnerLabel = new JLabel(result.getWinnerName() + AFTER_WINNER_TEXT);
		JButton continueButton = new JButton(CONTINUE_BUTTON_TEXT);
		JButton exitButton = new JButton(EXIT_BUTTON_TEXT);
		
		winnerLabel.setBounds(LABEL_RECTANGLE);
		continueButton.setBounds(LBUTTON_RECTANGLE);
		exitButton.setBounds(RBUTTON_RECTANGLE);
		
		this.add(winnerLabel);
		this.add(continueButton);
		this.add(exitButton);
		
		winnerLabel.setVisible(true);
		continueButton.setVisible(true);
		exitButton.setVisible(true);
		
		this.addWindowListener(
				new WindowListener() {
					@Override public void windowActivated(WindowEvent arg0) {}
					@Override public void windowClosed(WindowEvent e) {}
					@Override public void windowClosing(WindowEvent e) {
						client.exitGame();							
					}
					@Override public void windowDeactivated(WindowEvent e) {}
					@Override public void windowDeiconified(WindowEvent e) {}
					@Override public void windowIconified(WindowEvent e) {}
					@Override public void windowOpened(WindowEvent e) {}				
				}
			);
		
		continueButton.addMouseListener(
				new MouseListener() {
					@Override public void mouseClicked(MouseEvent arg0) {
						client.continueGame(); 
					}
					@Override public void mouseEntered(MouseEvent arg0) {}
					@Override public void mouseExited(MouseEvent arg0) {}
					@Override public void mousePressed(MouseEvent arg0) {}
					@Override public void mouseReleased(MouseEvent arg0) {}				
				}
			);
		
		exitButton.addMouseListener(
				new MouseListener() {
					@Override public void mouseClicked(MouseEvent arg0) { 
						client.exitGame(); 
					}
					@Override public void mouseEntered(MouseEvent arg0) {}
					@Override public void mouseExited(MouseEvent arg0) {}
					@Override public void mousePressed(MouseEvent arg0) {}
					@Override public void mouseReleased(MouseEvent arg0) {}				
				}
			);		
	}
}
